/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sunspotworld;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;

import rpc.connection.StreamedRPC;
import rpc.listener.RPCLED;
import rpc.listener.RPCServo;
import rpc.messenger.SwitchMessenger;
import rpc.messenger.VoltageMessenger;
import sensor.VoltageSensor;
import sensor.spot.SensorboardSwitch;
import sensor.spot.SpotADC;
import actuator.instance.GRTServo;
import actuator.instance.TriLED;

/**
 * 
 * @author ajc
 */
public class Main {

	public static void main(String[] args) throws IOException {

		InputStream in = Connector.openInputStream("serial://usb");
		OutputStream out = Connector.openOutputStream("serial://usb");
		StreamedRPC hsc = new StreamedRPC(in, out);
		hsc.start();

		// sensors
		// HWSwitch sw = new SensorboardSwitch(0, 5, "sw");
		// sw.start();
		//
		// SwitchMessenger m = new SwitchMessenger(43, sw, hsc);
		// m.startListening();

		// VoltageSensor a0 = new SpotADC(0, 20, "a0");
		// a0.start();
		//
		// VoltageMessenger m = new VoltageMessenger(43, a0, hsc);
		// m.startListening();

//		SensorboardSwitch s = new SensorboardSwitch(0, 50, "sw");
//		s.start();
//
//		SwitchMessenger sm = new SwitchMessenger(43, s, hsc);
//		sm.startListening();



		GRTServo s = new GRTServo(0);
		s.start();

		RPCServo rpcservo = new RPCServo(hsc, 12, s);
		rpcservo.startListening();
	}

	public static void start() throws IOException {
		InputStream in = Connector.openInputStream("serial://usb");
		OutputStream out = Connector.openOutputStream("serial://usb");
		StreamedRPC hsc = new StreamedRPC(in, out);
		hsc.start();

		// sensors
		// HWSwitch sw = new SensorboardSwitch(0, 5, "sw");
		// sw.start();
		//
		// SwitchMessenger m = new SwitchMessenger(43, sw, hsc);
		// m.startListening();

		VoltageSensor a0 = new SpotADC(0, 20, "a0");
		a0.start();

		VoltageMessenger m = new VoltageMessenger(43, a0, hsc);
		m.startListening();

		// actuators
		TriLED led = new TriLED(0);
		led.start();

		RPCLED rpcled = new RPCLED(hsc, 11, led);
		rpcled.startListening();

		// servo
		GRTServo s = new GRTServo(0);
		s.start();

		RPCServo rpcservo = new RPCServo(hsc, 12, s);
		rpcservo.startListening();
	}
}
